﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Win32;

namespace CSP.Package.Controls
{
    public partial class ExcelLocations : UserControl, IInstallationControl
    {
        public ExcelLocations()
        {
            InitializeComponent();
        }

        #region IInstallationControl Members

        protected Installer _form;
        public Installer GetForm()
        {
            return _form;
        }

        public void SetForm(Installer installer)
        {
            _form = installer;
        }

        private string GetInternetWebApplicationUrl()
        {
            if (RegistryAccess.PropertyExists(Registry.LocalMachine, Constants.CspRegistryKey, "CSPInternetWebApplication"))
                return RegistryAccess.GetStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "CSPInternetWebApplication");
            else
                return string.Empty;
        }

        private string GetIntranetWebApplicationUrl()
        {
            if (RegistryAccess.PropertyExists(Registry.LocalMachine, Constants.CspRegistryKey, "CSPIntranetWebApplication"))
                return RegistryAccess.GetStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "CSPIntranetWebApplication");
            else
                return string.Empty;
        }

        public bool Next()
        {
            try
            {

                Process p = new Process();
                p.StartInfo.FileName = @"ExcelLocations\AddTrustedFileLocation.bat";
                string arguments = string.Format(@"{0}", new string[] { GetInternetWebApplicationUrl()});

                GetForm().AddToLog(labels.CspInstallLog_Arguments+": " + arguments);

                p.StartInfo.Arguments = arguments;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.Start();
                string pepe = p.StandardOutput.ReadToEnd();
                this.GetForm().Hide();
                p.WaitForExit();
                this.GetForm().Show();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
        }

        public void Previous()
        {
        }

        public void Skip()
        {
        }

        #endregion

        private void ExcelLocations_Load(object sender, EventArgs e)
        {
            TranslateLiterals();
        }

        private void TranslateLiterals()
        {
            label1.Text = CSP.Package.labels.ExcelLocations_Intro;
        }
    }
}
